package com.huadao.cart_ccs.common;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import com.google.gson.Gson;
import com.huadao.cart_ccs.data.bean.DeviceInfo;
import com.huadao.cart_ccs.data.bo.PeristalticPumpBO;
import com.huadao.cart_ccs.main.CartApplication;

/**
 * 记录所有设备的参数信息到本地，在运行项目时，默认使用上一次记录的设备参数
 */
public class ParamsPreferences {
    private final static String sp_name= "environment";
    //sharePreferences 的 Key
    private final static String Key_CO2= "CO2";
    private final static String Key_O2= "O2";
    private final static String Key_temperature= "temperature";
    private final static String Key_fluidIntakePumpSpeed= "fluidIntakePump_Speed";
    private final static String Key_circulatingPumpSpeed= "circulatingPump_Speed";
    private final static String Key_wastePumpSpeed= "wastePump_Speed";
    private final static String Key_reclaimPumpSpeed= "reclaimPump_Speed";
    private final static String Key_fluidIntakePumpDirection= "fluidIntakePump_Direction";
    private final static String Key_circulatingPumpDirection= "circulatingPump_Direction";
    private final static String Key_wastePumpDirection= "wastePump_Direction";
    private final static String Key_reclaimPumpDirection= "reclaimPump_Direction";
    private final static String Key_cultureTankSpeed= "cultureTank_Speed";
    private final static String Key_stirrerSpeed= "stirrer_Speed";
    private final static String Key_changeAirFrequency= "changeAir_Frequency";
    private final static String Key_changeAirAmount= "changeAir_Amount";
    private final static String Key_ad= "ad";
    private final static String Key_ReferenceWeight="referenceWeight";
    private final static String key_DeviceInfo="DeviceInfo";
    private final static String Key_weightAdjustA="weightAdjustA";
    private final static String Key_weightAdjustB="weightAdjustB";
    private final static String Key_temperatureAdjust="temperatureAdjust";
    private final static String Key_shakeTableTime="shakeTableTime";

    //------------------------------默认值
    private final static int default_stirrerSpeed=100;
    private final static double default_temperature=37.0;
    private final static double default_CO2=5.0;
    private final static double default_O2=21.0;
    private final static int default_fluidIntakePumpSpeed=80;
    private final static int default_circulatingPumpSpeed=80;
    private final static int default_wastePumpSpeed=20;
    private final static int default_reclaimPumpSpeed=80;
    private final static byte default_fluidIntakePumpDirection= PeristalticPumpBO.directionReverse;
    private final static byte default_circulatingPumpDirection= PeristalticPumpBO.directionReverse;
    private final static byte default_wastePumpDirection= PeristalticPumpBO.directionForward;
    private final static byte default_reclaimPumpDirection= PeristalticPumpBO.directionReverse;
    private final static int default_cultureTankSpeed=50;
    private final static int default_changeAirFrequency=3600;//秒/次
    private final static int default_changeAirAmount=240;//ml/次
    private final static byte[] default_ad=new byte[]{0,0,0,0};//ml/次
    private final static double default_referenceWeight=0;
    private final static double default_weightAdjustA=1;
    private final static double default_weightAdjustB=0;
    private final static double default_temperatureAdjust=0;
    private final static int default_shakeTableTime=30;


    private static SharedPreferences preferences = CartApplication.getInstance().getSharedPreferences(sp_name, Context.MODE_PRIVATE);
    private static SharedPreferences.Editor editor = preferences.edit();

    private ParamsPreferences() {
    }

    private static class SingletonHolder {
        public final static ParamsPreferences instance = new ParamsPreferences();
    }

    public static ParamsPreferences getInstance() {
        return SingletonHolder.instance;
    }

    //-------------------------------CO2
    public double getCO2() {
        return Double.longBitsToDouble(preferences.getLong(Key_CO2, Double.doubleToRawLongBits(default_CO2)));
    }

    public void setCO2(double CO2) {
        editor.putLong(Key_CO2, Double.doubleToRawLongBits(CO2));
    }

    public void setCO2Commit(double CO2) {
        setCO2(CO2);
        commit();
    }

    //-------------------------------O2
    public double getO2() {
        return Double.longBitsToDouble(preferences.getLong(Key_O2, Double.doubleToRawLongBits(default_O2)));
    }

    public void setO2(double O2) {
        editor.putLong(Key_O2, Double.doubleToRawLongBits(O2));
    }

    public void setO2Commit(double O2) {
        setO2(O2);
        commit();
    }

    //-------------------------------温度
    public double getTemperature() {
        return Double.longBitsToDouble(preferences.getLong(Key_temperature, Double.doubleToRawLongBits(default_temperature)));
    }

    public void setTemperature(double temperature) {
        editor.putLong(Key_temperature, Double.doubleToRawLongBits(temperature));
    }

    public void setTemperatureCommit(double temperature) {
        setTemperature(temperature);
        commit();
    }

    //-------------------------------进液泵 转速
    public int getFluidIntakePumpSpeed(){
        return preferences.getInt(Key_fluidIntakePumpSpeed,default_fluidIntakePumpSpeed);
    }

    public void setFluidIntakePumpSpeed(int speed){
        editor.putInt(Key_fluidIntakePumpSpeed,speed);
    }

    public void setFluidIntakePumpSpeedCommit(int speed){
        setFluidIntakePumpSpeed(speed);
        commit();
    }

    //-------------------------------进液泵 方向
    public byte getFluidIntakePumpDirection(){
        return (byte) preferences.getInt(Key_fluidIntakePumpDirection,default_fluidIntakePumpDirection);
    }

    public void setFluidIntakePumpDirection(byte direction){
        editor.putInt(Key_fluidIntakePumpDirection,direction);
    }

    public void setFluidIntakePumpDirectionCommit(byte direction){
        setFluidIntakePumpDirection(direction);
        commit();
    }

    //-------------------------------进液泵 转速 方向
    public void setFluidIntakePump(int speed,byte direction){
        setFluidIntakePumpSpeed(speed);
        setFluidIntakePumpDirection(direction);
    }

    public void setFluidIntakePumpCommit(int speed,byte direction){
        setFluidIntakePump(speed,direction);
        commit();
    }

    //-------------------------------循环泵 转速
    public int getCirculatingPumpSpeed(){
        return preferences.getInt(Key_circulatingPumpSpeed,default_circulatingPumpSpeed);
    }

    public void setCirculatingPumpSpeed(int speed){
        editor.putInt(Key_circulatingPumpSpeed,speed);
    }

    public void setCirculatingPumpSpeedCommit(int speed){
        setCirculatingPumpSpeed(speed);
        commit();
    }

    //-------------------------------循环泵 方向
    public byte getCirculatingPumpDirection(){
        return (byte) preferences.getInt(Key_circulatingPumpDirection,default_circulatingPumpDirection);
    }

    public void setCirculatingPumpDirection(byte direction){
        editor.putInt(Key_circulatingPumpDirection,direction);
    }

    public void setCirculatingPumpDirectionCommit(byte direction){
        setCirculatingPumpDirection(direction);
        commit();
    }

    //-------------------------------循环泵 转速 方向
    public void setCirculatingPump(int speed,byte direction){
        setCirculatingPumpSpeed(speed);
        setCirculatingPumpDirection(direction);
    }

    public void setCirculatingPumpCommit(int speed,byte direction){
        setCirculatingPump(speed,direction);
        commit();
    }

    //-------------------------------废液泵 转速
    public int getWastePumpSpeed(){
        return preferences.getInt(Key_wastePumpSpeed,default_wastePumpSpeed);
    }

    public void setWastePumpSpeed(int speed){
        editor.putInt(Key_wastePumpSpeed,speed);
    }

    public void setWastePumpSpeedCommit(int speed){
        setWastePumpSpeed(speed);
        commit();
    }

    //-------------------------------废液泵 方向
    public byte getWastePumpDirection(){
        return (byte) preferences.getInt(Key_wastePumpDirection,default_wastePumpDirection);
    }

    public void setWastePumpDirection(byte direction){
        editor.putInt(Key_wastePumpDirection,direction);
    }

    public void setWastePumpDirectionCommit(byte direction){
        setWastePumpDirection(direction);
        commit();
    }

    //-------------------------------废液泵 转速 方向
    public void setWastePump(int speed,byte direction){
        setWastePumpSpeed(speed);
        setWastePumpDirection(direction);
    }

    public void setWastePumpCommit(int speed,byte direction){
        setWastePump(speed,direction);
        commit();
    }

    //-------------------------------罐装泵 转速
    public int getReclaimPumpSpeed(){
        return preferences.getInt(Key_reclaimPumpSpeed,default_reclaimPumpSpeed);
    }

    public void setReclaimPumpSpeed(int speed){
        editor.putInt(Key_reclaimPumpSpeed,speed);
    }

    public void setReclaimPumpSpeedCommit(int speed){
        setReclaimPumpSpeed(speed);
        commit();
    }
    //-------------------------------罐装泵 方向
    public byte getReclaimPumpDirection(){
        return (byte) preferences.getInt(Key_reclaimPumpDirection,default_reclaimPumpDirection);
    }

    public void setReclaimPumpDirection(byte direction){
        editor.putInt(Key_reclaimPumpDirection,direction);
    }

    public void setReclaimPumpDirectionCommit(byte direction){
        setReclaimPumpDirection(direction);
        commit();
    }
    //-------------------------------罐装泵 转速 方向
    public void setReclaimPump(int speed,byte direction){
        setReclaimPumpSpeed(speed);
        setReclaimPumpDirection(direction);
    }

    public void setReclaimPumpCommit(int speed,byte direction){
        setReclaimPump(speed,direction);
        commit();
    }

    //-------------------------------培养罐换气泵
    public int getCultureTankSpeed(){
        return preferences.getInt(Key_cultureTankSpeed,default_cultureTankSpeed);
    }

    public void setCultureTankSpeed(int speed){
        editor.putInt(Key_cultureTankSpeed,speed);
    }

    public void setCultureTankSpeedCommit(int speed){
        setCultureTankSpeed(speed);
        commit();
    }

    //-------------------------------磁力搅拌器
    public int getStirrerSpeed(){
        return preferences.getInt(Key_stirrerSpeed,default_stirrerSpeed);
    }

    public void setStirrerSpeed(int speed){
        editor.putInt(Key_stirrerSpeed,speed);
    }

    public void setStirrerSpeedCommit(int speed){
        setStirrerSpeed(speed);
        commit();
    }

    //-----------------------------换气频率
    public int getChangeAirFrequency(){
        return preferences.getInt(Key_changeAirFrequency,default_changeAirFrequency);
    }

    public void setChangeAirFrequency(int frequency){
        editor.putInt(Key_changeAirFrequency,frequency);
    }

    public void setChangeAirFrequencyCommit(int frequency){
        setChangeAirFrequency(frequency);
        commit();
    }

    //-------------------------换气量
    public int getChangeAirAmount(){
        return preferences.getInt(Key_changeAirAmount,default_changeAirAmount);
    }

    public void setChangeAirAmount(int amount){
        editor.putInt(Key_changeAirAmount,amount);
    }

    public void setChangeAirAmountCommit(int amount){
        setChangeAirAmount(amount);
        commit();
    }

    //---------------------去皮AD值
    public byte[] getAD(){
        String adStr=preferences.getString(Key_ad,null);
        if(adStr!=null){
            return Base64.decode(adStr, Base64.DEFAULT);
        }else {
            return default_ad;
        }
    }

    public void setAD(byte[] ad){
        String adStr=Base64.encodeToString(ad, Base64.DEFAULT);
        editor.putString(Key_ad,adStr);
    }

    public void setADCommit(byte[] ad){
        setAD(ad);
        commit();
    }

    //---------------------称重参考值
    public double getReferenceWeight(){
        return Double.longBitsToDouble(preferences.getLong(Key_ReferenceWeight, Double.doubleToRawLongBits(default_referenceWeight)));
    }

    public void setReferenceWeight(double weight){
        editor.putLong(Key_ReferenceWeight, Double.doubleToRawLongBits(weight));
    }

    public void setReferenceWeightCommit(double weight){
        setReferenceWeight(weight);
        commit();
    }

    //---------------------称重校准参数a
    public double getWeightAdjustA(){
        return Double.longBitsToDouble(preferences.getLong(Key_weightAdjustA, Double.doubleToRawLongBits(default_weightAdjustA)));
    }

    public void setWeightAdjustA(double weight){
        editor.putLong(Key_weightAdjustA, Double.doubleToRawLongBits(weight));
    }

    public void setWeightAdjustACommit(double weight){
        setWeightAdjustA(weight);
        commit();
    }

    //---------------------称重校准参数b
    public double getWeightAdjustB(){
        return Double.longBitsToDouble(preferences.getLong(Key_weightAdjustB, Double.doubleToRawLongBits(default_weightAdjustB)));
    }

    public void setWeightAdjustB(double weight){
        editor.putLong(Key_weightAdjustB, Double.doubleToRawLongBits(weight));
    }

    public void setWeightAdjustBCommit(double weight){
        setWeightAdjustB(weight);
        commit();
    }

    //---------------------温度校准参数adjust
    public double getTemperatureAdjust(){
        return Double.longBitsToDouble(preferences.getLong(Key_temperatureAdjust, Double.doubleToRawLongBits(default_temperatureAdjust)));
    }

    public void setTemperatureAdjust(double adjust){
        editor.putLong(Key_temperatureAdjust, Double.doubleToRawLongBits(adjust));
    }

    public void setTemperatureAdjustCommit(double adjust){
        setTemperatureAdjust(adjust);
        commit();
    }

    //---------------------摇床时间
    public int getShakeTableTime(){
        return preferences.getInt(Key_shakeTableTime,default_shakeTableTime);
    }

    public void setShakeTableTime(int seconds){
        editor.putInt(Key_shakeTableTime,seconds);
    }

    public void setShakeTableTimeCommit(int seconds){
        setShakeTableTime(seconds);
        commit();
    }

    //----------------------设备信息
    public DeviceInfo getDeviceInfo(){
        String jsonString=preferences.getString(key_DeviceInfo,null);
        if(jsonString==null){
            return null;
        }else {
            DeviceInfo deviceInfo = new Gson().fromJson(jsonString, DeviceInfo.class);
            return deviceInfo;
        }
    }

    public void setDeviceInfo(DeviceInfo deviceInfo){
        String jsonString=new Gson().toJson(deviceInfo);
        editor.putString(key_DeviceInfo,jsonString);
    }

    public void setDeviceInfoCommit(DeviceInfo deviceInfo){
        setDeviceInfo(deviceInfo);
        commit();
    }

    //-------------------------------
    public void commit() {
        editor.commit();
    }

    public void clear() {
        editor.clear().commit();
    }
}
